<template>
	<view class="Register_content Memory_key">
		<view class="register_box major_box">
			<view class="register_title">
				<image src="../../static/image/major_back_icon.png" @tap="back"></image>
				<text style="color:#C2C9DF;" :style="i18n.registertitlestyle">{{i18n.bfzlcregister_title}}</text>
			</view>
			
			<view class="tips_box">
				<text class="tips_title" :style="i18n.bfzltips_titlestyle">{{i18n.bfzltips_title}}</text>
				<view class="text_item">
					<text  style="width:98%">{{i18n.ps}}</text>
				</view>
				<view class="keyworld">
					<text v-for="(item,index) in mnemonic" :key="index">
						{{item}}
					</text>
					
				</view>
			</view>
			
			<view class="attention">
				<view class="attention_item">
					<image src="../../static/image/camera.png"></image>
					<text>{{i18n.attention_item1}}</text>
					<text>{{i18n.attention_item2}}</text>
				</view>
				<view class="attention_item">
					<image src="../../static/image/msg.png"></image>
					<text>{{i18n.attention_item3}}</text>
					<text>{{i18n.attention_item4}}</text>
				</view>
				<view class="attention_item">
					<image src="../../static/image/transfor.png"></image>
					<text>{{i18n.attention_item5}}</text>
					<text>{{i18n.attention_item6}}</text>
				</view>
				<view class="attention_item allow">
					<image src="../../static/image/writedown.png"></image>
					<text>{{i18n.attention_item7}}</text>
					<text>{{i18n.attention_item8}}</text>
				</view>
			</view>
			
			
			<image :src="i18n.netbtnimg" class="backups next_btn" @tap="next"></image>
		
		</view>
	</view>
</template>

<script>
	import { ethers } from 'ethers';
	import EthCrypto from 'eth-crypto';
	import * as Bip from 'bip39';
	import pbkdf2_1 from "pbkdf2";
	import HDKey from 'hdkey';
	import * as ethUtil from 'ethereumjs-util'


	export default {
		computed: {
			i18n() {
				return this.$t('user')
			}
		},
		data() {
			return {
				mnemonic:"",//助记词
				Wallet:"",
			}
		},
		onLoad() {
			//生成助记词
			uni.showLoading();
			this.getWords();
		},
		methods: {
			back(){
				this.$CommonJS.navigateBack()
			},
			next(){
				const  _this=this
				if(this.mnemonic.length < 1){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '助记词不完整':'Incomplete mnemonics')
					return;
				}
				const mnemonic = JSON.stringify(this.mnemonic);
				// this.$CommonJS.navigateTo('./memory_world_verification?mnemonic=' + mnemonic)
			uni.redirectTo({
							url: './memory_world_verification?mnemonic=' + mnemonic
			});
			},
			bytes2Str(arr) {
			    var str = "";
			    for(var i=0; i<arr.length; i++) {
						var tmp = arr[i].toString(16);
						if(tmp.length == 1) {
								tmp = "0" + tmp;
			      }
						str += tmp;
			    }
			    return str;
			},
			getWords(){
				// let rand = ethers.utils.randomBytes(16);
				// console.log(rand)
				// let mnemonic = ethers.utils.HDNode.entropyToMnemonic(rand);   // 生成助记词
				// console.log(mnemonic)
				const identity = EthCrypto.createIdentity();
				//console.log(identity.privateKey)
				const pkey = identity.privateKey.slice(2)
				//console.log(pkey)
				//console.log(Bip)
				//const key =  Bip.entropyToMnemonic(pkey);
				const mnemonic2 = Bip.generateMnemonic(); //generates string
				//const a =  Bip.mnemonicToSeed(mnemonic2,'123')
				const mnemonicBuffer = Buffer.from((mnemonic2 || '').normalize('NFKD'), 'utf8');
				const saltBuffer = Buffer.from('mnemonic'.normalize('NFKD'), 'utf8');
				const a =	pbkdf2_1.pbkdf2Sync(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512');
				const root = HDKey.fromMasterSeed(a);
				//const masterPrivateKey = root.privateKey.toString('hex');
				
				//const root = hdkey.fromMasterSeed(seed);
				const addrNode = root.derive("m/44'/550'/0'/0/0"); 
				console.log(this.bytes2Str(addrNode.privateKey))
				const privateKey = this.bytes2Str(addrNode.privateKey);
				const pubKey = ethUtil.privateToPublic(addrNode.privateKey);
				console.log(mnemonic2)
				this.mnemonic = mnemonic2.split(' ');
				console.log("04" + this.bytes2Str(pubKey))
				const publicKey = "0x04" + this.bytes2Str(pubKey);
				const  _this=this
				if(this.mnemonic.length < 1){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '助记词生成失败':'Mnemonic generation failed');
					return;
				}
				
				const Wallet = {
					mnemonic:mnemonic2,
					privateKey:privateKey,
					publicKey:publicKey
				}
				this.$store.commit('setWallet',Wallet);
				uni.hideLoading();
				// let rand = ethers.utils.randomBytes(16);
				// // 生成助记词
				// let hahah = ethers.utils.HDNode.entropyToMnemonic(rand);
				// console.log(hahah);
				return;
				// const  Wallet =  ethers.Wallet.createRandom(); //创建一个新的随机钱包
				// this.$store.commit('setWallet',Wallet);
				// const mnemonic =( Wallet.mnemonic.phrase).split(' ');
				// uni.hideLoading();
				// if(mnemonic.length < 1){
				// 	this.$CommonJS.showToast("助记词生成失败")
				// }else{
				// 	this.mnemonic =( Wallet.mnemonic.phrase).split(' ');
				// }
			}
		}
	}
</script>

<style lang="stylus">
	@import "../../static/stylus/register/memory.stylus"
</style>
